package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemBlockDataBinding;

import java.util.List;

/**
 * 屏蔽设置适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/12 16:09
 **/
public class BlockDataAdapter extends BaseBindingRecyclerViewAdapter<ItemBlockDataBinding, String> implements View.OnClickListener {
    public BlockDataAdapter(Context context) {
        super(context);
    }

    public BlockDataAdapter(@NonNull Context context, @NonNull List<String> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemBlockDataBinding> holder, @NonNull ItemBlockDataBinding b, int position, String item) {
        b.tvLabel.setText(findLabel(item));

        b.tvRemove.setTag(item);
        b.tvRemove.setOnClickListener(this);
    }

    /**
     * @return 是否应用斑马纹配色
     */
    @Override
    protected boolean useZebraStripe() {
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_remove) {
            String t = (String) v.getTag();
            int i = data.indexOf(t);
            if (i != -1) {
                data.remove(t);
                notifyItemRemoved(i);
            }
        }
    }

    private static String findLabel(String item) {
        if (item.contains("/")) {
            return item.split("/")[1];
        }
        return item;
    }
}
